package com.gopay.bis.web.controller.ebank;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gopay.remote.cashier.bis.CashierBgNotifyRemoteService;
import com.gopay.remote.cashier.vo.CashierBgNotifyRespMsg;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.gopay.bis.common.util.IpAddrUtil;
import com.gopay.bis.common.util.SendSmsUtils;
import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.util.HttpTransport;
import com.gopay.remote.bank.BankInteractRemoteService;

public abstract class RefundBankResBase extends BankResBase {
    private static Log logger = LogFactory.getLog(RefundBankResBase.class);
    
    @Resource(name = "bankInteractService")
    private BankInteractRemoteService bankInteractService;

    @Resource(name = "notifyCashier")
    private HttpTransport notifyCashier;

    @Resource(name = "cashierBgNotifyRemoteService")
    private CashierBgNotifyRemoteService cashierBgNotifyRemoteService;
    
    @Resource(name = "smsConfig")
    private Map<String, String> smsConfig;

    /**
     * 验证退款返回参数
     * 
     * @param request
     * @return
     */
    protected abstract boolean checkRefundParameters(HttpServletRequest request);

    /**
     * 构建支付类退款对象
     * 
     * @param orderId
     * @return
     */
    protected abstract BaseMessage buildRefundMessage(HttpServletRequest request);

    /**
     * 退款通知入口
     * 
     * @param request
     * @param response
     * @return
     */
    protected void executeRefund(HttpServletRequest request, HttpServletResponse response) {
        // 打印请求参数
        this.displayRequestParameters(request, logger);
        // 校验参数
        boolean isCheck = checkRefundParameters(request);
        try {
            if (isCheck) {
                // 退款返回处理
                BaseMessage baseMessage = buildRefundMessage(request);
                Result result = bankInteractService.refundBack(baseMessage);
                Map<String, String> param = (Map<String, String>) result.getModel("isNotifyCashier");
                param.put("sign", signRes(param)); // 后台通知cashier签名数据
                logger.error("notify cashier param:" + param);
                String gopayOrderID = param.get("orderId");
                logger.error(gopayOrderID + " Referer: " + request.getHeader("Referer") + " Ip: "
                        + IpAddrUtil.getRemoteRealIpAddr(request));

                if (result.isSuccess()) {
                    if ((Boolean) result.getDefaultModel()) {
                        String retCode = param.get("retCode"); // 交易状态
                        if ("00".equals(retCode) || "01".equals(retCode)) {
                            // 明确退款交易成功或失败状态，通知cashier账务处理
                            /*String res = notifyCashier.submit(param);
                            logger.error(gopayOrderID + "res =refund notifyCashier.submit(param):" + res);*/ //生产优化10期 lx 2017-9-30 16:23:37
                            CashierBgNotifyRespMsg respMsg= cashierBgNotifyRemoteService.callBgNotify(param);
                            logger.error(gopayOrderID + "cashierBgNotifyRemoteService.callBgNotify(param);:" + respMsg.getRespMsg());
                        }
                        // 构建响应银行参数
                        OrderInfo orderInfo = buildOrderInfo(request, result);
                        // 通知银行
                        if (orderInfo.getIsNotify()) {
                            notifyBank(response, orderInfo);
                        }
                    } else if (!(Boolean) result.getDefaultModel()) {
                        // 验签失败
                        logger.error("平台退款订单[" + gopayOrderID + "]验签失败 " + ToStringBuilder.reflectionToString(result));
                    }
                } else {
                    // 处理失败
                    logger.error("平台退款订单[" + gopayOrderID + "]处理失败: " + ToStringBuilder.reflectionToString(result));
                    if (StringUtils.equals(result.getResultCode(), ProcCodeConstants.PROC_CODE_100E5079)) {
                        SendSmsUtils.sendSmsErr(smsConfig.get("switch"), smsConfig.get("mobile"), "注意!!!订单金额不一致,银行:"
                                + baseMessage.getBank() + "orderId:" + gopayOrderID);
                    }
                }
            } else {
                logger.error("bank executeRefund notify parameters is null");
            }
        } catch (Exception e) {
            logger.error("BankResBase executeRefund error：", e);
        }
    }

    @Override
    protected BaseMessage buildMessage(HttpServletRequest request) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected OrderInfo buildOrderInfo(HttpServletRequest request, Result result) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected boolean checkParameters(HttpServletRequest request) {
        // TODO Auto-generated method stub
        return false;
    }

}
